package com.company.oa.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.workflow.dto.document.DocumentDTO;
import com.company.oa.workflow.entity.document.Document;
import com.company.oa.workflow.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/workflow/document")
public class DocumentController {
    
    @Autowired
    private DocumentService documentService;
    
    /**
     * 分页查询公文列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('workflow:document:list')")
    public Result<IPage<Document>> getPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Document document) {
        IPage<Document> pageResult = documentService.getPage(page, size, document);
        return Result.success(pageResult);
    }
    
    /**
     * 获取公文详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('workflow:document:info')")
    public Result<Map<String, Object>> getDocumentDetail(@PathVariable Long id) {
        Map<String, Object> detail = documentService.getDocumentDetail(id);
        return Result.success(detail);
    }
    
    /**
     * 创建公文（草稿）
     */
    @PostMapping
    @PreAuthorize("hasAuthority('workflow:document:create')")
    public Result<Boolean> createDocument(@RequestBody @Valid DocumentDTO documentDTO) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = documentService.createDocument(documentDTO, userId);
        return Result.success(success);
    }
    
    /**
     * 更新公文
     */
    @PutMapping
    @PreAuthorize("hasAuthority('workflow:document:update')")
    public Result<Boolean> updateDocument(@RequestBody @Valid DocumentDTO documentDTO) {
        boolean success = documentService.updateDocument(documentDTO);
        return Result.success(success);
    }
    
    /**
     * 提交公文
     */
    @PostMapping("/submit/{id}")
    @PreAuthorize("hasAuthority('workflow:document:submit')")
    public Result<Boolean> submitDocument(@PathVariable Long id) {
        boolean success = documentService.submitDocument(id);
        return Result.success(success);
    }
    
    /**
     * 取消公文
     */
    @PostMapping("/cancel/{id}")
    @PreAuthorize("hasAuthority('workflow:document:cancel')")
    public Result<Boolean> cancelDocument(@PathVariable Long id) {
        boolean success = documentService.cancelDocument(id);
        return Result.success(success);
    }
    
    /**
     * 上传附件
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('workflow:document:upload')")
    public Result<String> uploadAttachment(@RequestParam("file") MultipartFile file) {
        String filePath = documentService.uploadAttachment(file);
        return Result.success(filePath);
    }
    
    /**
     * 获取我的公文列表
     */
    @GetMapping("/my")
    public Result<IPage<Document>> getMyDocuments(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        IPage<Document> pageResult = documentService.getMyDocuments(page, size, userId, status);
        return Result.success(pageResult);
    }
    
    /**
     * 签发公文
     */
    @PostMapping("/issue/{id}")
    @PreAuthorize("hasAuthority('workflow:document:issue')")
    public Result<Boolean> issueDocument(@PathVariable Long id) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = documentService.issueDocument(id, userId);
        return Result.success(success);
    }
    
    /**
     * 归档公文
     */
    @PostMapping("/archive/{id}")
    @PreAuthorize("hasAuthority('workflow:document:archive')")
    public Result<Boolean> archiveDocument(@PathVariable Long id) {
        boolean success = documentService.archiveDocument(id);
        return Result.success(success);
    }
    
    /**
     * 传阅公文
     */
    @PostMapping("/circulate/{id}")
    @PreAuthorize("hasAuthority('workflow:document:circulate')")
    public Result<Boolean> circulateDocument(@PathVariable Long id, @RequestBody Long[] userIds) {
        boolean success = documentService.circulateDocument(id, userIds);
        return Result.success(success);
    }
} 